<template>
    <div class="middle-box text-center loginscreen animated fadeInDown">
        <div class="load-ele login-box">
            <div>
                <h1 class="logo-name">CCB</h1>
            </div>
            <h3 class="m-b-lg">后台管理平台</h3>
            <h6>登录</h6>
            <form class="m-t" @submit.prevent="login">
                <div class="form-group" :class="{'has-error':pwdHasError}">
                    <input type="text" class="form-control" placeholder="请输入帐号" v-model="username">
                </div>
                <div class="form-group" :class="{'has-error':pwdHasError}">
                    <input type="password" class="form-control" placeholder="请输入密码" v-model="password">
                </div>
                <button class="btn btn-primary block full-width" type="submit">登 录</button>
            </form>
        </div>
    </div>
</template>
<script>
    import {tip,ajax} from '../tools/tools'
    import {cfg} from '../config/config'

    export default {
        data: function () {
            return {
                username: '',
                unHasError: false,
                password: '',
                pwdHasError: false
            }
        },
        watch:{
        },
        methods: {
            login: function () {
                var self = this;
                var isPass = true;
                self.unHasError = false;
                self.pwdHasError = false;
                if ($.trim(self.username).length == 0) {
                    self.unHasError = true;
                    isPass = false;
                }
                if ($.trim(self.password).length == 0) {
                    self.pwdHasError = true;
                    isPass = false;
                }
                if (isPass) {
                    var par = {
                        login: self.username,
                        pwd: self.password,
                        type:0
                    }


                    // ajax.testLogin({
                    //     param:{},
                    //     cb(data){
                    //         self.$root.TOKEN = localStorage.token = data.data;
                    //     }
                    // })

                    ajax.login({
                        param: par,
                        cb:function(data){
                            if(data.rcode==cfg.rcode.SUCC){
                                self.$root.TOKEN = localStorage.token = data.data.token;
                                self.$root.USER_TYPE = localStorage.user_type = data.data.type;
                                self.$root.USER_NAME = localStorage.user_name = self.username;
                                self.$root.$router.replace({
                                    name:'index'
                                });
                            } else {
                                tip.err(data.message);
                            }
                            
                            
                        },
                        failCb:function(data){

                        }
                    });
                } else {
                    tip.err('请输入用户名,密码,验证码');
                }
            }
        },
        mounted(){
        }
    }
</script>